package com.yjxxt.gradeLevel;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Date;

// 接收Text，DoubleWriteable ，输出类型为Student ， NullWritable
public class StudentReducer extends Reducer<Text, DoubleWritable , Student, NullWritable> {

    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Reducer<Text, DoubleWritable, Student, NullWritable>.Context context) throws IOException, InterruptedException {
        /**
         *
         * 将总成绩分为5个等级，分别是GradeE：[0,60)分，GradeD：[60,70)分 ，
         * GradeC： [70,80)分，GradeB：[80,90)分，GradeA：[90,100]分。
         *
         */
        int GradeA=0,GradeB=0,GradeC=0,GradeD=0,GradeE=0;  // 每个等级初始人数为零

        for (DoubleWritable value : values) {
            if(value.get()>0&&value.get()<60){
                GradeE++;
            }else if(value.get()<70){
                GradeD++;
            }else if(value.get()<80){
                GradeC++;
            }else if(value.get()<90){
                GradeB++;
            }else{
                GradeA++;
            }
        }
        // id gender grade count createtime
        // 分别输出GradeA ， GradeB ， GradeC ， GradeD ， GradeE
        Student student1=new Student("0",key.toString(), "GradeA",GradeA,new Date());
        Student student2=new Student("0",key.toString(), "GradeB",GradeB,new Date());
        Student student3=new Student("0",key.toString(), "GradeC",GradeC,new Date());
        Student student4=new Student("0",key.toString(), "GradeD",GradeD,new Date());
        Student student5=new Student("0",key.toString(), "GradeE",GradeE,new Date());

        context.write(student1,NullWritable.get());
        context.write(student2,NullWritable.get());
        context.write(student3,NullWritable.get());
        context.write(student4,NullWritable.get());
        context.write(student5,NullWritable.get());


    }
}
